package com.zhongshengnetwork.rightbe.lang.view;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Parcelable;
//import android.support.annotation.Nullable;
//import android.support.v4.app.Fragment;
//import android.support.v4.app.FragmentPagerAdapter;
//import android.support.v4.view.PagerAdapter;
//import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.zhongshengnetwork.rightbe.Adv.model.AdvertUtils;
import com.zhongshengnetwork.rightbe.Adv.model.SPManageUtils;
import com.zhongshengnetwork.rightbe.CustomApplication;
import com.zhongshengnetwork.rightbe.MainActivity;
import com.zhongshengnetwork.rightbe.PageOneFragment;
import com.zhongshengnetwork.rightbe.R;
import com.zhongshengnetwork.rightbe.clerk.view.ClerkFragment;
import com.zhongshengnetwork.rightbe.clerk.view.ClerkRecyclerFragment;
import com.zhongshengnetwork.rightbe.common.BaseAnalyticsFragment;
import com.zhongshengnetwork.rightbe.common.CommonUtils;
import com.zhongshengnetwork.rightbe.wzt.view.WZTFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luojingxing on 2018/6/13.
 */

public class HomeLangFragment extends BaseAnalyticsFragment implements View.OnClickListener{

    private View rootView;
    private ViewPager mPaper;
    private FragmentPagerAdapter mAdapter;
    private List<Fragment> mFragments = null;
    private TextView care_update_circle;
    private TextView tv_one,tv_two,tv_three,tv_four, tv_five,tv_six,tv_seven,tv_eight;
    private LinearLayout tv_one_line,tv_two_line,tv_three_line,tv_four_line, tv_five_line,tv_six_line,tv_seven_line,tv_eight_line;
    private CareLangFragment careLangFragment;
    private HotLangFragment hotLangFragment;
    private LatestLangFragment latestLangFragment;
    private ClerkFragment clerkFragment;
    //private ClerkRecyclerFragment clerkRecyclerFragment;
    private TopicFragment topicFragment;
    private ShareLangFragment heartFragment;
    private ShareLangFragment lifeFragment;
    private WZTFragment wztFragment;
    public MainActivity mainActivity;
    private int currentPosition = 0;

    public void updateAfterLogin(){
        /*
        if (careLangFragment != null){
            careLangFragment.getData();
        }
        */
    }

    public void updateCareLangTip(){
        if (care_update_circle != null){
            String tip = SPManageUtils.getInstance(CustomApplication.mContext).getSPString(AdvertUtils.CareUpdateKey,"0");
            if (tip.equals("1") && CommonUtils.isLogin() == true){
                care_update_circle.setVisibility(View.VISIBLE);
            }else {
                care_update_circle.setVisibility(View.INVISIBLE);
            }
        }
    }

    public void setFragmentOfIndex(int index){
        if (getActivity() == null){
            return;
        }
        if (mPaper == null){
            return;
        }
        resetColor();
        setSelect(index);
        mPaper.setCurrentItem(index);
        currentPosition = index;
        getFirstData(index, false);
    }

    public void scrollToFirstData(){
        getFirstData(currentPosition, true);
    }

    private void getFirstData(int index, boolean isFirst){
        if (index == 0){
            if (hotLangFragment != null){
                hotLangFragment.getFirstData(isFirst);
            }
        }else if (index == 1){
            if (careLangFragment != null){
                careLangFragment.getFirstData(isFirst);
            }
        }else if (index == 2){
            if (latestLangFragment != null){
                latestLangFragment.getFirstData(isFirst);
            }
        }else if (index == 3){

            if (clerkFragment != null){
                clerkFragment.getFirstData(isFirst);
            }
            /*
            if (clerkRecyclerFragment != null){
                clerkRecyclerFragment.getFirstData(isFirst);
            }
            */
        }else if (index == 4){
            if (wztFragment != null){
                wztFragment.getFirstData(isFirst);
            }
        }else if (index == 5){
            if (lifeFragment != null){
                lifeFragment.getFirstData(isFirst);
            }
        }else if (index == 6){
            if (heartFragment != null){
                heartFragment.getFirstData(isFirst);
            }
        }else if (index == 7){
            if (topicFragment != null){
                topicFragment.getFirstData(isFirst);
            }
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //System.out.print("首页界面创建\n");
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.langfragment, container,false);
            initLayout();
            if (mAdapter == null){
                mAdapter = new FragmentPagerAdapter(getChildFragmentManager()) {
                    @Override
                    public int getCount() {
                        return mFragments.size();
                    }

                    @Override
                    public Fragment getItem(int arg0) {
                        Fragment fragment = mFragments.get(arg0);
                        return fragment;
                    }

                };
            }
            mPaper.setAdapter(mAdapter);
            mPaper.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

                private int currentIndex = 0;

                @Override
                public void onPageSelected(int position) {
                    resetColor();
                    setSelect(position);
                    currentIndex = position;
                    if (currentIndex == 1){
                        if (careLangFragment != null){
                            careLangFragment.getLatest();
                        }
                    }
                }

                @Override
                public void onPageScrolled(int arg0, float arg1, int arg2) {

                }

                @Override
                public void onPageScrollStateChanged(int arg0) {

                }
            });
            updateCareLangTip();

        }else {
            //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }
        return rootView;
    }


    private void setSelect(int position){
        if (tv_one == null || tv_one_line == null || tv_two == null || tv_two_line == null || tv_three == null || tv_three_line == null
                || tv_four == null || tv_four_line == null || tv_five == null || tv_five_line == null || tv_six == null || tv_six_line == null
         || tv_eight == null || tv_eight_line == null){
            return;
        }
        currentPosition = position;
        switch (position) {
            case 0://FC6E51
                hotLangFragment.getFirstData(false);
                tv_one.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_one_line.setVisibility(View.VISIBLE);
                break;
            case 1:
                careLangFragment.getFirstData(false);
                tv_two.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_two_line.setVisibility(View.VISIBLE);
                break;
            case 2:
                latestLangFragment.getFirstData(false);
                tv_three.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_three_line.setVisibility(View.VISIBLE);
                break;
            case 3:
                clerkFragment.getFirstData(false);
                //clerkRecyclerFragment.getFirstData(false);
                tv_four.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_four_line.setVisibility(View.VISIBLE);
                break;
            case 4:
                wztFragment.getFirstData(false);
                tv_five.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_five_line.setVisibility(View.VISIBLE);
                break;
            case 5:
                lifeFragment.getFirstData(false);
                tv_six.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_six_line.setVisibility(View.VISIBLE);
                break;
            case 6:
                heartFragment.getFirstData(false);
                tv_seven.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_seven_line.setVisibility(View.VISIBLE);
                break;
            case 7:
                topicFragment.getFirstData(false);
                tv_eight.setTextColor(Color.rgb(0x99,0xcc,0x00));
                tv_eight_line.setVisibility(View.VISIBLE);
                break;
            default:
                break;
        }
    }

    /**
     * 初始化控件
     */
    public void initLayout(){
        tv_one = (TextView)rootView.findViewById(R.id.tv_one);
        tv_one_line = (LinearLayout) rootView.findViewById(R.id.tv_one_line);
        tv_two = (TextView)rootView.findViewById(R.id.tv_two);
        tv_two_line = (LinearLayout) rootView.findViewById(R.id.tv_two_line);
        tv_three = (TextView)rootView.findViewById(R.id.tv_three);
        tv_three_line = (LinearLayout)rootView.findViewById(R.id.tv_three_line);
        care_update_circle = (TextView) rootView.findViewById(R.id.care_update_circle);
        tv_four = (TextView)rootView.findViewById(R.id.tv_four);
        tv_four_line = (LinearLayout)rootView.findViewById(R.id.tv_four_line);
        tv_five = (TextView) rootView.findViewById(R.id.tv_five);
        tv_five_line = (LinearLayout) rootView.findViewById(R.id.tv_five_line);
        tv_six = (TextView) rootView.findViewById(R.id.tv_six);
        tv_six_line = (LinearLayout) rootView.findViewById(R.id.tv_six_line);
        tv_seven = (TextView) rootView.findViewById(R.id.tv_seven);
        tv_seven_line = (LinearLayout) rootView.findViewById(R.id.tv_seven_line);
        tv_eight = (TextView) rootView.findViewById(R.id.tv_eight);
        tv_eight_line = (LinearLayout) rootView.findViewById(R.id.tv_eight_line);
        mPaper = (ViewPager)rootView.findViewById(R.id.view_pager);

        tv_one.setOnClickListener(this);
        tv_two.setOnClickListener(this);
        tv_three.setOnClickListener(this);
        tv_four.setOnClickListener(this);
        tv_five.setOnClickListener(this);
        tv_six.setOnClickListener(this);
        tv_seven.setOnClickListener(this);
        tv_eight.setOnClickListener(this);
        if (mFragments == null){
            mFragments = new ArrayList<Fragment>();
            hotLangFragment = new HotLangFragment();
            hotLangFragment.mainActivity = mainActivity;
            mFragments.add(hotLangFragment);

            careLangFragment = new CareLangFragment();
            careLangFragment.mainActivity = mainActivity;
            mFragments.add(careLangFragment);

            latestLangFragment = new LatestLangFragment();
            latestLangFragment.mainActivity = mainActivity;
            mFragments.add(latestLangFragment);

            clerkFragment = new ClerkFragment();
            clerkFragment.mainActivity = mainActivity;
            mFragments.add(clerkFragment);

            /*
            clerkRecyclerFragment = new ClerkRecyclerFragment();
            mFragments.add(clerkRecyclerFragment);
            */

            wztFragment = new WZTFragment();
            wztFragment.dataType = 1;
            mFragments.add(wztFragment);

            lifeFragment = new ShareLangFragment();
            lifeFragment.isLangType = false;
            lifeFragment.getType = 9;
            mFragments.add(lifeFragment);

            heartFragment = new ShareLangFragment();
            heartFragment.isLangType = true;
            heartFragment.getType = 44;
            mFragments.add(heartFragment);

            topicFragment = new TopicFragment();
            topicFragment.type = 1;
            mFragments.add(topicFragment);

        }

    }

    public void resetColor(){
        if (tv_one == null || tv_one_line == null || tv_two == null || tv_two_line == null || tv_three == null || tv_three_line == null
                || tv_four == null || tv_four_line == null || tv_five == null || tv_five_line == null || tv_six == null || tv_six_line == null){
            return;
        }
        
        tv_one.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_one_line.setVisibility(View.INVISIBLE);
        tv_two.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_two_line.setVisibility(View.INVISIBLE);
        tv_three.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_three_line.setVisibility(View.INVISIBLE);
        tv_four.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_four_line.setVisibility(View.INVISIBLE);
        tv_five.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_five_line.setVisibility(View.INVISIBLE);
        tv_six.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_six_line.setVisibility(View.INVISIBLE);
        tv_seven.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_seven_line.setVisibility(View.INVISIBLE);
        tv_eight.setTextColor(Color.rgb(0x75,0x7d,0x88));
        tv_eight_line.setVisibility(View.INVISIBLE);
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_one:
                resetColor();
                setSelect(0);
                mPaper.setCurrentItem(0);
                break;
            case R.id.tv_two:
                resetColor();
                setSelect(1);
                mPaper.setCurrentItem(1);
                if (careLangFragment != null){
                    careLangFragment.getLatest();
                }
                break;
            case R.id.tv_three:
                resetColor();
                setSelect(2);
                mPaper.setCurrentItem(2);
                break;
            case R.id.tv_four:
                resetColor();
                setSelect(3);
                mPaper.setCurrentItem(3);
                break;
            case R.id.tv_five:
                resetColor();
                setSelect(4);
                mPaper.setCurrentItem(4);
                break;
            case R.id.tv_six:
                resetColor();
                setSelect(5);
                mPaper.setCurrentItem(5);
                break;
            case R.id.tv_seven:
                resetColor();
                setSelect(6);
                mPaper.setCurrentItem(6);
                break;
            case R.id.tv_eight:
                resetColor();
                setSelect(7);
                mPaper.setCurrentItem(7);
                break;
            default:
                resetColor();
                setSelect(0);
                mPaper.setCurrentItem(0);
                break;
        }
    }
    /**
     * ViewPager适配器
     */
    public class MyPagerAdapter extends PagerAdapter {
        public List<Activity> mListViews;

        public MyPagerAdapter(List<Activity> mListViews) {
            this.mListViews = mListViews;
        }


        @Override
        public void finishUpdate(View arg0) {
        }

        @Override
        public int getCount() {
            return mListViews.size();
        }



        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
        }
    }

}
